{% extends "djforum/base.html" %}

{% load i18n staticfiles pagination_tags bootstrap forum_tags %}

{% block title %}{{ topic }} | {{ block.super }}{% endblock %}

{% block head %}
    {{ block.super }}
    {{ form.media }}
    <script>
    jQuery(function($){
        $('.post-form').submit(function(){
            if ( ! $('textarea', this).val()){
                alert('{% trans "Enter message" %}');
                return false;
            }
        });

        $('.answer-post').click(function(){
            var $post = $(this).parents('.post').find('.post-content').clone();
            $post.find('blockquote').remove();

            var text = $.trim($post.text());
            text = '>'+text.replace(/(\r\n|\n|\r)/gm, "\n\n>")

            $('#id_body').focus().val($('#id_body').val()+text+'\n\n');
            return false;
        });

        $('.delete-post').click(function(){
            return confirm('{% trans "Do you wish delete this post?" %}');
        });

        $('.delete-topic').click(function(){
           return confirm('{% trans "Do you wish delete this topic?" %}');
        })

        $('.close-topic').click(function(){
           return confirm('{% if topic.closed %}{% trans "Do you wish open this topic?" %}{% else %}{% trans "Do you wish close this topic?" %}{% endif %}');
        })

        $('.heresy-topic').click(function(){
           return confirm('{% if topic.heresy %}{% trans "do you wish remove heresy mark for this topic?"|capfirst %}{% else %}{% trans "do you wish mark this topic as heresy?"|capfirst %}{% endif %}');
        })

        $('.stick-topic').click(function(){
           return confirm('{% if topic.sticky %}{% trans "Do you wish unstick this topic?" %}{% else %}{% trans "Do you wish stick this topic?" %}{% endif %}');
        })

        $('.js-answer-topic').click(function(){
            $('#id_body').focus();
            return false;
        });
    });
    </script>
{% endblock %}

{% block content %}
    <aside class="widget forum">

        <ul class="breadcrumb">
            <li><a class="title" href="{{ forum.category.get_absolute_url }}">{{ forum.category }}</a> <span class="divider">/</span></li>
            <li><a class="title forum" href="{{ forum.get_absolute_url }}">{{ forum }}</a> <span class="divider">/</span></li>
            <li class="active">{{ topic }}</li>
        </ul>

        <div class="topic-status pull-left">
            {% include 'djforum/_topic_statuses.html' %}
        </div>

        <div class="pull-right topic-toolbar">
            {% if user.is_authenticated %}
                {% with unread_count=user|unread_topics_count %}
                <a href="{% url 'forum:unread_topics' %}" class="btn btn-small">
                    {% trans "Unread topics" %}{% if unread_count %} ({{ unread_count }}){% endif %}
                </a>
                {% endwith %}
            {% endif %}

            {% if topic|can_edit:user %}
            <div class="btn-group">
                <a class="btn btn-small btn-info dropdown-toggle" data-toggle="dropdown" href="#">
                    {{ _("manage")|title }}
                    <i class="fa fa-caret-down"></i>
                </a>
                <ul class="dropdown-menu">
                    <li>
                        <a href="{% url 'forum:move_topic' topic.pk %}">
                            {% trans 'Move' %}
                        </a>
                    </li>
                    <li>
                        <a href="{% url 'forum:heresy_unheresy_topic' topic.pk %}" class="heresy-topic">
                            {% if topic.heresy %}{% trans "not heresy"|capfirst %}{% else %}{% trans "heresy"|capfirst %}{% endif %}
                        </a>
                    </li>
                    <li>
                        <a href="{% url 'forum:stick_unstick_topic' topic.pk %}" class="stick-topic">
                            {% if topic.sticky %}{% trans "Unstick" %}{% else %}{% trans "Stick" %}{% endif %}
                        </a>
                    </li>
                    <li>
                        <a href="{% url 'forum:close_open_topic' topic.pk %}" class="close-topic">
                            {% if topic.closed %}{% trans "Open" %}{% else %}{% trans "Close" %}{% endif %}
                        </a>
                    </li>
                <ul>
            </div>
            {% endif %}

            {% if topic|can_delete:user %}
                <form action="{% url 'forum:delete_topic' topic.pk %}" method="post" style="display: inline">
                    {% csrf_token %}
                    <button class="btn btn-small btn-danger delete-topic">{% trans "Delete" %}</button>
                </form>
            {% endif %}

            <a href="{% url 'forum:add_post' topic.pk %}" class="js-answer-topic btn btn-small">{% trans "Answer" %}</a>

            {% if topic.user == user %}
                {% if topic.send_response %}
                    <a href="{% url 'forum:unsubscribe' topic.pk %}" class="btn btn-info btn-small">{% trans "Unsubscribe" %}</a>
                {% else %}
                    <a href="{% url 'forum:subscribe' topic.pk %}" class="btn btn-info btn-small">{% trans "Subscribe" %}</a>
                {% endif %}
            {% endif %}
        </div>

        {% if is_paginated %}
            {% paginate float='left' %}
        {% endif %}

        {% for post in object_list %}
            {% if forloop.counter == 1 %}
                {% include "djforum/_post.html" with topic_rating='True' %}
            {% else %}
                {% include "djforum/_post.html" %}
            {% endif %}
            {% if forloop.counter == 5 %}{% include "_yandex.html" %}{% endif %}
            {% if forloop.last and object_list|length > 10 %}{% include "_yandex.html" with index="999" %}{% endif %}
        {% endfor %}

        {% if is_paginated %}
            {% paginate float='left' %}
        {% endif %}

        {% if form %}
        <form action="{% url 'forum:add_post' topic.pk %}" method="post" class="post-form form-horizontal">
            {% csrf_token %}
            <button type="submit" class="btn btn-small submit-button">{% trans "Submit" %}</button>
            {{ form.body }}
            <p>
                {% blocktrans with url='//daringfireball.net/projects/markdown/syntax' %}
                Use <a href="{{ url }}">Markdown</a> to format text.
                {% endblocktrans %}
            </p>
        </form>
        {% endif %}
    </aside>
{% endblock %}
